<?php

namespace app\merchant\controller;

use think\Request;

class Activity extends Common
{
    protected $activity;

    public function _initialize()
    {
        parent::_initialize();
        $this->activity = new \app\common\model\Activity();
    }

    /**
     * 活动列表页面
     */
	public function listActivity(){
        $merchant_id = session('merchant.merchant_id');
        $page = input('page/n',1);
        $pagesize = input('DataTables_Table_0_length/n', 10);
	    $list=$this->activity->getStoreActivityList($merchant_id,$pagesize);
	    $this->assign([
	        'activity_list' => $list,
            'page' => $page,
        ]);
		return view('activitylist');
	}

    /**
     * 平台活动列表页面
     */
    public function listPingTaiActivity(){
        $merchant_id = session('merchant.merchant_id');
        $pagesize = input('DataTables_Table_0_length/n', 10);
        $page = input('page/n',1);
        $list=$this->activity->getActivityList($pagesize);
        $this->assign([
            'activity_list' => $list,
            'page' => $page,
        ]);
        return view('ptactivitylist');
    }

    /**
     * 报名平台活动页面
     */
    public function joinActivity(){
        $merchant_id = session('merchant.merchant_id');
        $page = input('page/n',1);
        if(Request::instance()->method() == 'POST'){
            $req_data = Request::instance()->param();
            $req_data = $req_data['data'];
            $req_data['store_id']=$merchant_id;
            $res = $this->activity->handleJoinActivity($req_data);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"报名成功",'data'=>$res));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"报名失败，请稍后再试"));
            }
            exit();
        }
        $id = input('id/n', 0);
        $join_data=$this->activity->getJoinActivityInfo($id);
        $storeinfo = $this->activity->getStoreInfo($merchant_id);
        $goodslist = $this->activity->getGoodsList($merchant_id,1);
        $total_page = $this->activity->getGoodsPage($merchant_id);
        $this->assign([
            'goodslist'=>$goodslist,
            'storeinfo'=>$storeinfo,
            'total_page'=>$total_page,
            'page'=>$page,
            'join_activity' => $join_data,
        ]);
        return view('joinactivity');
    }

    /**
     * 添加活动页面
     */
	public function addActivity(){
        $merchant_id = session('merchant.merchant_id');
        if(Request::instance()->method() == 'POST'){
            $page= input("page/n",1);
            $goodslist = $this->activity->getGoodsList($merchant_id,$page);
            if(!empty($goodslist)){
                echo json_encode(array('status'=>1,'msg'=>"获取成功",'data'=>$goodslist));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"添加失败"));
            }
            exit();
        }
        $storeinfo = $this->activity->getStoreInfo($merchant_id);
        $goodslist = $this->activity->getGoodsList($merchant_id,1);
        $total_page = $this->activity->getGoodsPage($merchant_id);
	    $this->assign([
            'goodslist'=>$goodslist,
	        'total_page'=>$total_page,
	        'storeinfo'=>$storeinfo,
        ]);
		return view("addactivity");
	}

    /**
     * 添加活动数据
     */
	public function createActivity(){
        if(Request::instance()->method() == 'POST'){
            $req_data = Request::instance()->param();
            $req_data = $req_data['data'];
            $req_data['create_id'] = session('merchant.merchant_id');
            $req_data['start_time'] = strtotime($req_data['start_time']." 00:00:00");
            $req_data['end_time'] = strtotime($req_data['end_time']." 23:59:59");
            $req_data['use_type'] = 1;
            $res = $this->activity->insertActivity($req_data);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"添加成功"));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"添加失败"));
            }
            exit();
        }else{
            echo json_encode(array('status'=>0,'msg'=>"数据请求错误"));
        }
    }

    /**
     * 更新活动数据
     */
    public function updateActivity(){
        if(Request::instance()->method() == 'POST'){
            $req_data = Request::instance()->param();
            $req_data = $req_data['data'];
            $req_data['start_time'] = strtotime($req_data['start_time']." 00:00:00");
            $req_data['end_time'] = strtotime($req_data['end_time']." 23:59:59");
            $res = $this->activity->updateActivity($req_data);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"更新成功"));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"更新失败"));
            }
            exit();
        }
        $activity_id =input("id/n");
        $activityinfo = $this->activity->getActivityInfo($activity_id);
        $merchant_id = session('merchant.merchant_id');
        $storeinfo = $this->activity->getStoreInfo($merchant_id);
        $goodslist = $this->activity->getDetailGoodsList($merchant_id);
        $act_goods = [];
        foreach ($goodslist as $k => $v){
            if($v['goods_activity_id'] == $activity_id){
                $act_goods[] = $goodslist[$k];
            }
        }
        if(empty($act_goods)){
            $show_btn = 0;
        }else{
            $show_btn = 1;
        }
        $activityinfo['start_time'] = date("Y-m-d",$activityinfo['start_time']);
        $activityinfo['end_time'] = date("Y-m-d",$activityinfo['end_time']);
        $this->assign([
            'goodslist'=>$goodslist,
            'show_btn'=>$show_btn,
            'act_goods'=>$act_goods,
            'storeinfo'=>$storeinfo,
            'activityinfo'=>$activityinfo,
        ]);
        return view('updateactivity');
    }

    /**
     * 展示详情活动数据
     */
    public function detailActivity(){
        $activity_id =input("id/n");
        $page = input('page/n',1);
        $activityinfo = $this->activity->getActivityInfo($activity_id);
        $merchant_id = session('merchant.merchant_id');
        $storeinfo = $this->activity->getStoreInfo($merchant_id);
        $activityinfo['start_time'] = date("Y-m-d",$activityinfo['start_time']);
        $activityinfo['end_time'] = date("Y-m-d",$activityinfo['end_time']);
        $activityinfo['create_time'] = date("Y-m-d",$activityinfo['create_time']);
        $goodslist = $this->activity->getDetailGoodsList($merchant_id);
        $act_goods = [];
        foreach ($goodslist as $k => $v){
            if($v['goods_activity_id'] == $activity_id){
                $act_goods[] = $goodslist[$k];
            }
        }
        if(empty($act_goods)){
            $show_btn = 0;
        }else{
            $show_btn = 1;
        }
        $this->assign([
            'storeinfo'=>$storeinfo,
            'show_btn'=>$show_btn,
            'act_goods'=>$act_goods,
            'page'=>$page,
            'activityinfo'=>$activityinfo,
        ]);
        return view('detailactivity');
    }

    /**
     * 展示平台活动报名的详情
     */
    public function joindetail(){
        $merchant_id = session('merchant.merchant_id');
        $page = input('page/n',1);
        $id = input('id/n', 0);
        $join_data = $this->activity->getJoinActivityInfo($id);
        $goodslist = $this->activity->getJoinGoodsList($merchant_id,$id);
        $is_store_selfrate = 0;
        if(empty($goodslist)){
            $is_join = 0;
        }else{
            $is_join = 1;
            if($goodslist[0]['goods_activity_selfrate'] > 0){
                $is_store_selfrate = $goodslist[0]['goods_activity_selfrate'];
            }else{
                $is_store_selfrate = 0;
            }
        }
        $this->assign([
            'goodslist'=>$goodslist,
            'join_activity' => $join_data,
            'is_join' => $is_join,
            'page' => $page,
            'is_store_selfrate' => $is_store_selfrate,
        ]);
        return view('actgoodsdetail');
    }

    /**
     * 获取活动选择商品信息
     */
    public function getSelectGoods(){
        if(Request::instance()->method() == 'POST'){
            $ids=input("data/n");
            $result = $this->activity->getSelectGoods($ids);
            if(empty($result)) {
                echo json_encode(array('status' => 0, 'msg' => "获取失败"),JSON_UNESCAPED_UNICODE);
            }else{
                echo json_encode(array('status'=> 1, 'msg' => "获取成功",'data'=>$result),JSON_UNESCAPED_UNICODE);
            }
            exit();
        }
    }

    /**
     * 活动状态置为失效
     */
    public function deleteActivity(){
        if(Request::instance()->method() == 'POST'){
            $id=input("id/n");
            $merchant_id = session('merchant.merchant_id');
            $result = $this->activity->delactivity($id,$merchant_id);
            if($result == false) {
                echo json_encode(array('status' => 0, 'msg' => "删除失败"),JSON_UNESCAPED_UNICODE);
            }else{
                echo json_encode(array('status'=> 1, 'msg' => "删除成功",'data'=>$result),JSON_UNESCAPED_UNICODE);
            }
            exit();
        }
    }

}
